package com.File;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 测试FileInputStream和FileOutputStream的使用
 *
 *
 * 结论:
 *      1.对于文本文件(.txt,.doc,.java)，使用字符流处理。
 *      2.对于非文本文件(图片.jpg，视频.avi.mp4.ppt)，使用字节流处理。
 *
 *
 *
 *
 */
public class FileInputStreamOutputStreamTest {

    //使用字节流FileInputStream字节流来处理文本文件可能出现乱码.
    @Test
    public void testFileInputStream() throws IOException {

        //1.造文件
        File file = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\hello.txt");

        //2.造流
        FileInputStream fileInputStream = new FileInputStream(file);


        //3.读数据,在读取中文时偶尔会出现乱码，因为UTF-8编码中一个中文占3个字节，有可能会出现将一个汉子劈开的现象，产生乱码。
        byte[] buffer = new byte[5];
        int len;  //记录每次读取的字节的个数
        while ((len = fileInputStream.read(buffer)) != -1){

            String str = new String(buffer, 0, len);
            System.out.println(str);
        }

        //4.关闭资源
        fileInputStream.close();
    }

    //实现对图片的复制。
    @Test
    public void testFileInputOutputStream() {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //
            File srcFile = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG");
            File destFile = new File("D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian2.PNG");


            //
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);


            //复制的过程
            byte[] buffer = new byte[5];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                //关闭操作
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    //指定路径下文件的复制
    public void copyFile(String srcpath, String destPath){
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            //
            File srcFile = new File(srcpath);
            File destFile = new File(destPath);


            //
            fileInputStream = new FileInputStream(srcFile);
            fileOutputStream = new FileOutputStream(destFile);


            //复制的过程,通常这里写1024
            byte[] buffer = new byte[1024];
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

            try {
                //关闭操作
                fileInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void testCopyFile(){

        long start = System.currentTimeMillis();

        String srcpath = "D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian.PNG";

        String destpath = "D:\\xuehujavaSE01\\src\\main\\java\\com\\File\\tupian3.PNG";

        copyFile(srcpath, destpath);


        long stop = System.currentTimeMillis();

        System.out.println("复制操作花费的时间为" + (stop - start));


    }



}
